package datastructure.stack;

/**
 * @author XY
 * @version 1.0
 * @date 2022/2/21 9:20
 * @Description
 */
public class Leet155 {

    static class MinStack {

        int[] arr = null;
        int size = 0;
        int[] minArr = new int[1];
        int minSize = 0;

        public MinStack() {
            arr = new int[30000];
        }

        public void push(int val) {
            arr[size] = val;
            if(size == 0){
                minArr[size] = val;
                minSize++;
            }else if(val<minArr[minSize-1]){
                int[] newArr = new int[minSize+1];
                for(int j =0 ;j<minSize;j++){
                    newArr[j] = minArr[j];
                }
                newArr[newArr.length-1] = val;
                minArr = newArr;
                minSize++;
            }
            size++;
        }


        public void pop() {
            if(minArr[minSize-1] == arr[size-1]&&minSize>0){
                minSize--;
            }
            size--;
        }

        public int top() {
            return arr[size-1];
        }

        public int getMin() {
            return minArr[minSize-1];
        }
    }

    public static void main(String[] args) {
        MinStack minStack = new MinStack();
        minStack.push(0);
        minStack.push(1);
        minStack.push(0);
        System.out.println(minStack.getMin());
        minStack.pop();
        System.out.println(minStack.top());
        System.out.println(minStack.getMin());
    }


}
